package DrugManagement

import (
	"MedicalTreatment/global"
	model "MedicalTreatment/model"
	"github.com/gin-gonic/gin"
)

func DrugSelect(r *gin.Engine) {
	r.POST("/drugSelect", func(context *gin.Context) {
		var drug model.Drug
		if err := context.ShouldBindJSON(&drug); err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		var drugSel []model.Drug
		if drug.Name != "" {
			if err := global.DB.Where("name Like ?", drug.Name).Find(&drugSel).Error; err != nil {
				context.JSON(200, gin.H{
					"Msg": err,
				})
			} else {
				context.JSON(200, gin.H{
					"Msg": drugSel,
				})
			}
		} else {
			if err := global.DB.Find(&drugSel).Error; err != nil {
				context.JSON(200, gin.H{
					"Msg": err,
				})
			} else {
				context.JSON(200, gin.H{
					"Msg": drugSel,
				})
			}
		}

	})
}
